package com.md.api.controller;

import com.md.api.common.utils.StringUtils;
import com.md.api.entity.HttpResult;
import com.md.api.entity.ShoppingCart;
import com.md.api.entity.dto.ComposeCommitDto;
import com.md.api.entity.dto.ComposeQueryDto;
import com.md.api.service.CartSevice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RequestMapping("/cart")
@RestController
public class CartController {


	@Resource
	CartSevice cartSevice;




	//加入购物车
	@RequestMapping("/addShoppingCart")
	@ResponseBody
	public HttpResult addShoppingCart(HttpServletRequest request,@RequestBody ShoppingCart shoppingCart) {
		if (null==shoppingCart.getNumCount()|| StringUtils.isEmpty(shoppingCart.getSpuId())||StringUtils.isEmpty(shoppingCart.getSkuId())){
			return HttpResult.PARAM_ERROR;
		}
		return cartSevice.addShoppingCart(request,shoppingCart);
	}

	//我的购物车
	@RequestMapping("/myCart")
	@ResponseBody
	public HttpResult myCart(HttpServletRequest request,@RequestBody ComposeQueryDto qto) {
		return cartSevice.myCart(request,qto);
	}

	//移除购物车
	@RequestMapping("/removeProduct")
	@ResponseBody
	public HttpResult removeProduct(HttpServletRequest request,@RequestBody ComposeCommitDto qto) {
		if (StringUtils.isEmpty(qto.getCartId())){
			return HttpResult.PARAM_ERROR;
		}
		return cartSevice.removeProduct(request,qto);
	}


	@RequestMapping("/resetProductCount")
	@ResponseBody
	public HttpResult resetProductCount(HttpServletRequest request,@RequestBody ComposeCommitDto qto) {
		if (StringUtils.isEmpty(qto.getCartId())||null==qto.getCount()||qto.getCount()<1){
			return HttpResult.PARAM_ERROR;
		}
		return cartSevice.resetProductCount(request,qto);
	}
}
